// import commonData from './commonData'

export default {
  getSelectedLabel (value, options) {
    let obj = {}
    obj = options.find((item) => {
      return item.value === value
    })
    return obj.label
  },
  // getLabel(val, listName) {
  //     let list = listName;
  //     if ("object" !== typeof listName) {
  //         list = commonData[listName]
  //     }
  //     for (let i = 0; i < list.length; i++) {
  //         if (list[i].value == val) {
  //             return list[i].label
  //         }
  //     }
  //     return '-'
  // },
  getAreaLabel (p, c, a) {
    const province = chinaArea['86'][p]
    if (c == null || c == undefined) { return [province] }
    const city = chinaArea[p][c]
    // city = city == '市辖区' ? '' : city
    if (a == null || a == undefined) { return [province, city] }
    const area = chinaArea[c][a]
    return [province, city, area]
  },
  dateFomat (date, fmt) {
    const o = {
      'M+': date.getMonth() + 1, // 月份
      'd+': date.getDate(), // 日
      'h+': date.getHours(), // 小时
      'm+': date.getMinutes(), // 分
      's+': date.getSeconds(), // 秒
      'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
      S: date.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (const k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  },
  isCardNo (card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    return reg.test(card) !== false
  },
  // 时间处理
  getTimeFormat (nowtimetamp, format) {
    let nowtime
    if (nowtimetamp) {
      nowtime = new Date(parseInt(nowtimetamp))
    } else {
      nowtime = new Date()
    }
    const year = nowtime.getFullYear()
    const month = this.padleft0(nowtime.getMonth() + 1)
    const day = this.padleft0(nowtime.getDate())
    const hour = this.padleft0(nowtime.getHours())
    const minute = this.padleft0(nowtime.getMinutes())
    const second = this.padleft0(nowtime.getSeconds())
    let millisecond = nowtime.getMilliseconds()
    millisecond = millisecond.toString().length === 1 ? '00' + millisecond : millisecond.toString().length === 2 ? '0' + millisecond : millisecond
    return format.replace('YYYY', year).replace('MM', month).replace('DD', day).replace('hh', hour).replace('mm', minute).replace('ss', second)
  },
  // 补齐两位数
  padleft0 (obj) {
    return obj.toString().replace(/^[0-9]{1}$/, '0' + obj)
  },
  // byte数据单位转换（以B为基底）
  bytesToSize (bytes) {
    if (bytes === 0) return '0 B'
    const k = 1024 // or 1000
    const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))

    return (bytes / Math.pow(k, i)).toFixed(1) + ' ' + sizes[i]
  },
  // byte数据单位转换（以M为基底）
  megabytesToSize (bytes) {
    if (bytes === 0) return '0 MB'
    const k = 1024 // or 1000
    const sizes = ['MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))

    return (bytes / Math.pow(k, i)).toFixed(0) + ' ' + sizes[i]
  },
  // byte数据单位转换（以G为基底）
  gbytesToSize (bytes) {
    if (bytes === 0) return '0 GB'
    const k = 1024 // or 1000
    const sizes = ['GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
    const i = Math.floor(Math.log(bytes) / Math.log(k))

    return (bytes / Math.pow(k, i)).toFixed(0) + ' ' + sizes[i]
  },
  timeout (second) {
    const oneHour = 60 * 60
    const oneDay = oneHour * 24
    const oneYear = oneDay * 365
    return [
      parseInt(second / oneYear) + '年',
      parseInt(second % oneYear / oneDay) + '天',
      parseInt(second % oneDay / oneHour) + '小时',
      parseInt(second % oneHour / 60) + '分',
      parseInt(second % 60) + '秒'
    ]
      .join('')
      .replace(/\b(\d)\b/g, '0$1') // 自动补零
      .replace(/\b(\d+)\b/g, ' $1 ') // 数字两边加空格
      .replace(/(\s00\s([^\s]+))?/g, '') // 移除 00 时间
  },
  // 姓名脱敏
  hideName (str) {
    if (!str) {
      return ''
    }
    if (str.length <= 3) {
      return '*' + str.substring(1, str.length)
    } else if (str.length > 3 && str.length <= 6) {
      return '**' + str.substring(2, str.length)
    } else if (str.length > 6) {
      return str.substring(0, 2) + '****' + str.substring(6, str.length)
    }
  },

  // 手机号码脱敏
  hidePhone (phone) {
    if (!phone) {
      return ''
    }
    return phone.replace(/^(.{3})(?:\w+)(.{4})$/, '\$1****\$2')
  },

  // 身份证脱敏
  hideIdentity (identity) {
    if (!identity) {
      return ''
    }
    return identity.replace(/^(.)(?:\w+)(.)$/, '\$1****************\$2')
  },

  // 权限判断
  hasPermission (permission) {
    return window.permissions && window.permissions.indexOf(permission) > -1
  }

}
